package com.llcbenwu.controller;

import com.llcbenwu.mode.SocialUserInfo;
import com.llcbenwu.util.AppSignUpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.social.connect.Connection;
import org.springframework.social.connect.web.ProviderSignInUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.ServletWebRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author lilinchun
 * @date 2021/11/16 0016 15:32
 */
@RestController
public class AppSecurityController {


    @Autowired
    private ProviderSignInUtils providerSignInUtils;


    @Resource
    private AppSignUpUtils appSignUpUtils;

    /**
     *获取用户三方信息
     * @param httpServletRequest
     * @return
     */
    @GetMapping("/social/user")
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public SocialUserInfo getSocialUserInfo(HttpServletRequest httpServletRequest) {
        SocialUserInfo socialUserInfo=new SocialUserInfo();
        //获取session中的信息拿出来但是之后就拿不到
        Connection<?> connectionFromSession = providerSignInUtils.getConnectionFromSession(new ServletWebRequest(httpServletRequest));
        socialUserInfo.setHeadimg(connectionFromSession.getImageUrl());
        socialUserInfo.setNickname(connectionFromSession.getDisplayName());
        socialUserInfo.setProviderId(connectionFromSession.getKey().getProviderId());
        socialUserInfo.setProviderUserId(connectionFromSession.getKey().getProviderUserId());
        //将出入session中的信息放入redis中
        appSignUpUtils.saveConnectData(new ServletWebRequest(httpServletRequest),connectionFromSession.createData());
        return socialUserInfo;
    }
}
